#include "common.hpp"

int main()
{
    //获取共享内存id
    int shm_id = get_shm();

    //挂接共享内存
    char* shm_addr = (char*)shmat(shm_id, NULL, 0);
    if(shm_addr == (void*)-1)
    {
        log(Fatal, "shm_addr error:%s\n", strerror(errno));
        return -1;
    }

    //打开标志传送文件
    int fd = open(FIFO_FILE, O_WRONLY); //打开标志接收文件
    if (fd < 0)
    {
        log(Fatal, "Open myfifo error:%s", strerror(errno));
        exit(-1);
    }

    //通信
    while(true)
    {
        cout << "Please enter: ";
        fgets(shm_addr, SIZE_C, stdin);
        char c = 's';
        int ret = write(fd, &c, sizeof(char));
        if(ret < 0)
        {
            log(Fatal, "Write myfifo error:%s", strerror(errno));
            break;
        }
    }

    //detach共享内存
    close(fd);
    int ret = shmdt(shm_addr);
    if(ret < 0)
    {
        log(Fatal, "shmdt error:%s\n", strerror(errno));
        return -1;
    }

    return 0;
}